import { Component, OnInit,Injectable} from '@angular/core';
import {ActivatedRoute} from "@angular/router";
import {UserParams,ResponseList,User,Properties} from "../../entities/entities";
import {UserService} from "../../services/services";
import {Router} from "@angular/router"

@Component({
selector: 'list-component',
templateUrl: `ListComponent.html`,
styles  : [`ListComponent.scss`]
})
export class ListComponent implements OnInit {

  pageNum:number;
  pageSize:number;
  total:number;
  data:User[]=[];
  loading = true;


  // _allChecked:boolean;
  _deleting:boolean=false;
  constructor(private userService:UserService,private properties:Properties,private router:Router,private route:ActivatedRoute) {
  }


  ngOnInit() {
    var params:UserParams=new UserParams(//实际上只有 new 才在运行时才有强类型,用 ={} 或 =any 在运行时实际上不认识 左侧是谁 
      parseInt(this.route.params["_value"]["pageSize"]),
      parseInt(this.route.params["_value"]["pageNum"])
    );
    this.pageNum=params.pageNum;
    this.pageSize=params.pageSize;
    this.refresh();
  }


  refresh(isRefresh:boolean=false){
    isRefresh&&(this.pageNum=1);
    this.router.navigate([`/index/list/${this.pageNum}/${this.pageSize}`]);
    this.loading=true;
    this.userService.selectFromUser({
      pageNum:this.pageNum,
      pageSize:this.pageSize,
    }).then((responseList:ResponseList<User>)=>{
      this.total=responseList.total;
      this.loading=false;
      this.data=responseList.items;
    });
  }

  /**所有记录中选中状态去重后的集合*/
  _getCheckedFromSet():boolean[]{
    // return this.data.reduce((a:boolean,b:User)=>{
    //   if(a===null){
    //     a=b.checked;
    //   }else{
    //     a=(a&&!b.checked)||(!a&&b.checked);
    //   }
    //   return a;

    // },null);
    return Array.from(new Set(this.data.map((a:User)=>a.checked)));
  }

  /**点击全选的按钮*/
  _onCheckAllClick(isChecked:boolean){
    this.data.forEach((e:User)=>e.checked=isChecked);

  }



  /**删除或恢复数据*/
  _deleteOrRestoreDatas(isDelete:boolean){
    this._deleting=true;

    var paramUser:User=new User(null,null,null,null,null,null,null,null,null,!isDelete);

    this.userService.updateUserByAccounts(this.data.filter((e:User)=>e.checked).map((e:User)=>e.account),paramUser).then(()=>{
      this._deleting=false;
      this.refresh();
    });
  }
  /**改变数据有效性*/
  _changeActive(user:User){
    // console.log(user);
    var paramUser:User=new User(null,null,null,null,null,null,null,null,null,null);
    paramUser.active=user.active;
    this.userService.updateUserByAccounts([user.account],paramUser).then(()=>{
    }).catch((e:any)=>{
      user.active=!user.active;
      throw e;
    });    
  }


  /**是否全部选中*/
  _isAllChecked():boolean{
    return this.data.reduce((a:boolean,b:User)=>{return a&&b.checked},true);
  }

  /**是否部分选中*/
  _isPartChecked():boolean{
    return !this._isAllChecked()&&!this._isNoOneChecked();
  }
  /**是否一个都没选中*/
  _isNoOneChecked():boolean{
    return !this.data.reduce((a:boolean,b:User)=>{return a||b.checked},false);
  }

  /**是否选中一个*/
  _isOneChecked():boolean{
    return this.data.filter((a:User)=>a.checked).length===1;
  }





  /**是否全部数据有效*/
  _isAllActive():boolean{
    return this.data.reduce((a:boolean,b:User)=>{return a&&b.active},true);
  } 

  /**是否全部数据无效*/
  _isNoOneActive():boolean{
    return !this.data.reduce((a:boolean,b:User)=>{return a||b.active},false);

  }



}